using System;
using System.Collections.Generic;
using Voodoo.Sauce.Internal.Utils;

namespace Voodoo.Sauce.Internal.Analytics
{
	internal class AnalyticsStorageHelper : IAnalyticStorage
	{
		private const string GAME_COUNT_KEY = "VoodooSauce_GameCount";

		private const string SUCCESSFUL_GAME_COUNT_KEY = "VoodooSauce_SuccessfulGameCount";

		private const string CURRENT_LEVEL_KEY = "VoodooSauce_Level";

		private const string APP_LAUNCH_COUNT_KEY = "VoodooSauce_AppLaunchCount";

		private const string HIGHEST_SCORE_KEY = "VoodooSauce_HighScore";

		private const string FIRST_APP_LAUNCH_DATE_KEY = "VoodooSauce_FirstAppLaunchDate";

		private const string SHOW_INTERSTITIAL_COUNT_KEY = "FsCount";

		private const string SHOW_REWARDED_COUNT_KEY = "RvCount";

		private const string SHOW_APP_OPEN_COUNT_KEY = "AoCount";

		private const string SHOW_NATIVE_ADS_COUNT_KEY = "NativeAds_Ad_Count";

		private const string MREC_COUNT_KEY = "MRec_Ad_Count";

		private const string AUDIO_AD_COUNT_KEY = "Audio_Ad_Count";

		private const string INTERSTITIAL_WATCHED_COUNT_KEY = "VoodooSauce_FSShownCount";

		private const string REWARDED_WATCHED_COUNT_KEY = "VoodooSauce_RVShownCount";

		private const string APP_OPEN_WATCHED_COUNT_KEY = "VoodooSauce_AOShownCount";

		private const string SHOW_CROSS_PROMO_COUNT_KEY = "CrossPromoCount";

		private const string SHOW_BACKUP_FS_COUNT_KEY = "BackupFSCount";

		private const string SHOW_BACKUP_RV_COUNT_KEY = "BackupRVCount";

		private const string GAME_ROUND_ID_KEY = "GameRoundId";

		private const string RV_USED_KEY = "RvUsed";

		private static IAnalyticStorage instance;

		private readonly Dictionary<string, object> _cache = new Dictionary<string, object>();

		internal static IAnalyticStorage Instance => instance ?? (instance = new AnalyticsStorageHelper());

		public void PreLoad()
		{
			LoadCacheStorageIntValue("VoodooSauce_GameCount");
			LoadCacheStorageIntValue("RvCount");
			LoadCacheStorageIntValue("FsCount");
			LoadCacheStorageIntValue("AoCount");
			LoadCacheStorageIntValue("MRec_Ad_Count");
			LoadCacheStorageIntValue("NativeAds_Ad_Count");
			LoadCacheStorageStringValue("VoodooSauce_Level");
		}

		public int IncrementShowCrossPromoCount()
		{
			return IncrementIntValue("CrossPromoCount");
		}

		public int GetShowCrossPromoCount()
		{
			return GetIntValue("CrossPromoCount");
		}

		public void IncrementShowBackupFsCount()
		{
			IncrementIntValue("BackupFSCount");
		}

		public void IncrementShowBackupRvCount()
		{
			IncrementIntValue("BackupRVCount");
		}

		public int GetShowBackupFsCount()
		{
			return GetIntValue("BackupFSCount");
		}

		public int GetShowBackupRvCount()
		{
			return GetIntValue("BackupRVCount");
		}

		public void IncrementGameCount()
		{
			IncrementIntValue("VoodooSauce_GameCount");
		}

		public int GetGameCount()
		{
			return GetIntValue("VoodooSauce_GameCount");
		}

		public int IncrementSuccessfulGameCount()
		{
			return IncrementIntValue("VoodooSauce_SuccessfulGameCount");
		}

		public int GetSuccessfulGameCount()
		{
			return GetIntValue("VoodooSauce_SuccessfulGameCount");
		}

		public double GetWinRate()
		{
			double num = GetGameCount();
			double num2 = GetSuccessfulGameCount();
			return Math.Min((num > 0.0) ? (num2 / num) : 0.0, 1.0);
		}

		public bool HasWinRate()
		{
			if (NintendoPlayerPrefs.HasKey("VoodooSauce_GameCount"))
			{
				return NintendoPlayerPrefs.HasKey("VoodooSauce_SuccessfulGameCount");
			}
			return false;
		}

		public string GetCurrentLevel()
		{
			return GetStringValue("VoodooSauce_Level");
		}

		public void UpdateCurrentLevel(string level)
		{
			UpdateStringValue("VoodooSauce_Level", level);
		}

		public bool HasCurrentLevel()
		{
			return !string.IsNullOrEmpty(GetCurrentLevel());
		}

		public float GetGameHighestScore()
		{
			return GetFloatValue("VoodooSauce_HighScore");
		}

		public bool HasGameHighestScore()
		{
			return NintendoPlayerPrefs.HasKey("VoodooSauce_HighScore");
		}

		public bool UpdateGameHighestScore(float score)
		{
			if (score < GetGameHighestScore())
			{
				return false;
			}
			UpdateFloatValue("VoodooSauce_HighScore", score);
			return true;
		}

		public string GetGameRoundId()
		{
			if (!_cache.ContainsKey("GameRoundId"))
			{
				return null;
			}
			return (string)_cache["GameRoundId"];
		}

		public string CreateRoundId()
		{
			string text = Guid.NewGuid().ToString();
			_cache["GameRoundId"] = text;
			return text;
		}

		public int GetAppLaunchCount()
		{
			return GetIntValue("VoodooSauce_AppLaunchCount", 1);
		}

		public bool IsFirstAppLaunch()
		{
			return GetAppLaunchCount() == 1;
		}

		public void IncrementAppLaunchCount()
		{
			IncrementIntValue("VoodooSauce_AppLaunchCount");
		}

		public DateTime? GetFirstAppLaunchDate()
		{
			int intValue = GetIntValue("VoodooSauce_FirstAppLaunchDate");
			if (intValue <= 0)
			{
				return null;
			}
			return TimeUtils.TimeStampToDateTime(intValue);
		}

		public void SaveFirstAppLaunchDate()
		{
			UpdateIntValue("VoodooSauce_FirstAppLaunchDate", TimeUtils.NowAsTimeStamp());
		}

		public int IncrementAudioAdCount()
		{
			return IncrementIntValue("Audio_Ad_Count");
		}

		public int GetAudioAdCount()
		{
			return GetIntValue("Audio_Ad_Count");
		}

		public int IncrementShowInterstitialCount()
		{
			return IncrementIntValue("FsCount");
		}

		public int GetShowInterstitialCount()
		{
			return GetIntValue("FsCount");
		}

		public int IncrementShowRewardedVideoCount()
		{
			return IncrementIntValue("RvCount");
		}

		public int GetShowRewardedVideoCount()
		{
			return GetIntValue("RvCount");
		}

		public int IncrementShowAppOpenCount()
		{
			return IncrementIntValue("AoCount");
		}

		public int GetShowAppOpenCount()
		{
			return GetIntValue("AoCount");
		}

		public int IncrementMrecCount()
		{
			return IncrementIntValue("MRec_Ad_Count");
		}

		public int GetMrecCount()
		{
			return GetIntValue("MRec_Ad_Count");
		}

		public int IncrementShowNativeAdsCount()
		{
			return IncrementIntValue("NativeAds_Ad_Count");
		}

		public int GetShowNativeAdsCount()
		{
			return GetIntValue("NativeAds_Ad_Count");
		}

		public int IncrementInterstitialWatchedCount()
		{
			return IncrementIntValue("VoodooSauce_FSShownCount");
		}

		public int GetInterstitialWatchedCount()
		{
			return GetIntValue("VoodooSauce_FSShownCount");
		}

		public int IncrementRewardedVideoWatchedCount()
		{
			return IncrementIntValue("VoodooSauce_RVShownCount");
		}

		public int GetRewardedVideoWatchedCount()
		{
			return GetIntValue("VoodooSauce_RVShownCount");
		}

		public int IncrementAppOpenWatchedCount()
		{
			return IncrementIntValue("VoodooSauce_AOShownCount");
		}

		public int GetAppOpenWatchedCount()
		{
			return GetIntValue("VoodooSauce_AOShownCount");
		}

		public void IncreaseRvUsed()
		{
			_cache["RvUsed"] = GetRvUsed() + 1;
		}

		public void ResetRvUsed()
		{
			_cache["RvUsed"] = 0;
		}

		public int GetRvUsed()
		{
			if (_cache.ContainsKey("RvUsed"))
			{
				return (int)_cache["RvUsed"];
			}
			_cache["RvUsed"] = 0;
			return 0;
		}

		private int IncrementIntValue(string key, int defaultValue = 0)
		{
			int num = NintendoPlayerPrefs.GetInt(key, defaultValue) + 1;
			_cache[key] = num;
			NintendoPlayerPrefs.SetInt(key, num);
			return num;
		}

		private void UpdateIntValue(string key, int value)
		{
			_cache[key] = value;
			NintendoPlayerPrefs.SetInt(key, value);
		}

		private int GetIntValue(string key)
		{
			if (!_cache.ContainsKey(key))
			{
				return LoadCacheStorageIntValue(key);
			}
			return (int)_cache[key];
		}

		private int GetIntValue(string key, int @default)
		{
			if (!_cache.ContainsKey(key))
			{
				return LoadCacheStorageIntValue(key, @default);
			}
			return (int)_cache[key];
		}

		private int LoadCacheStorageIntValue(string key)
		{
			int @int = NintendoPlayerPrefs.GetInt(key);
			_cache[key] = @int;
			return @int;
		}

		private int LoadCacheStorageIntValue(string key, int @default)
		{
			int @int = NintendoPlayerPrefs.GetInt(key, @default);
			_cache[key] = @int;
			return @int;
		}

		private void UpdateFloatValue(string key, float value)
		{
			_cache[key] = value;
			NintendoPlayerPrefs.SetFloat(key, value);
		}

		private float GetFloatValue(string key)
		{
			if (!_cache.ContainsKey(key))
			{
				return LoadCacheStorageFloatValue(key);
			}
			return (float)_cache[key];
		}

		private float LoadCacheStorageFloatValue(string key)
		{
			float @float = NintendoPlayerPrefs.GetFloat(key);
			_cache[key] = @float;
			return @float;
		}

		private void UpdateStringValue(string key, string value)
		{
			_cache[key] = value;
			NintendoPlayerPrefs.SetString(key, value);
		}

		private string GetStringValue(string key)
		{
			if (!_cache.ContainsKey(key))
			{
				return LoadCacheStorageStringValue(key);
			}
			return (string)_cache[key];
		}

		private string LoadCacheStorageStringValue(string key)
		{
			string @string = NintendoPlayerPrefs.GetString(key);
			_cache[key] = @string;
			return @string;
		}
	}
}
